package com.project.forlearningdemo.controller;

import com.alibaba.fastjson.JSON;
import com.project.forlearningdemo.pojo.vo.ResponseInfo;
import com.project.forlearningdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

@Api(value = "UserController", tags = {"与用户有关的API"})
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Resource
    UserService userServiceImpl;

    @ApiOperation("查询用户信息 根据userId 或 username 或 mail查找用户")
    @PostMapping("query")
    public String findUser(@ApiParam(value = "userId 或 username 或 mail", required = true)@RequestBody  String user){
        return userServiceImpl.findUser(user);
    }

    @ApiOperation("修改用户信息 不可用来改密码或邮箱")
    @PostMapping("update")
    public String updateUser(@ApiParam(value = "根据邮箱mail 或者用户编号userId 修改用户信息", required = true)@RequestBody String user){
        return userServiceImpl.updateUser(user);
    }

    @ApiOperation("增加用户 没有必要使用，因为注册时已经可以创建用户信息，只需要修改用户信息即可")
    @PostMapping("add")
    public String addUser(@ApiParam(value = "用户信息", required = true)@RequestBody String user){
        return userServiceImpl.addUser(user);
    }

    @ApiOperation("删除用户 注销账号")
    @DeleteMapping("{userId}")
    public String deleteUser(@ApiParam(value = "用户编号", required = true) @PathVariable Integer userId){
        return userServiceImpl.deleteUser(userId);
    }

    @ApiOperation("用户注册  不作验证码判断处理")
    @PostMapping("reg")
    public String registerUser(@ApiParam(value = "邮箱号mail 和 密码password 和 验证码code", required = true) @RequestBody String mpc) throws MessagingException {
        return userServiceImpl.registerUser(mpc);
    }

    @ApiOperation("发送验证码 直接传入需要发送邮箱的即可")
    @PostMapping("code")
    public String getCode(@ApiParam(value = "邮箱号", required = true)@RequestBody String mail) throws MessagingException {
        return userServiceImpl.sendCode(mail);
    }

    @ApiOperation("用户登录 可以用username 或 mail登录，但是这个API需要将字段全部作为为username传过来，不要用mail")
    @PostMapping("/login")
    public String loginUser(@ApiParam(value = "账号username和密码password",required = true) @RequestBody String up, HttpServletRequest request){
        return userServiceImpl.loginUser(up, request);
    }

    @ApiOperation("忘记密码api")
    @PostMapping("wa")
    public String waJiMiMa(@ApiParam(value = "邮箱号mail 和 新密码password 和 验证码code",required = true)@RequestBody String mpc){
        return userServiceImpl.updateUserPass(mpc);
    }


    @ApiOperation("无权限：如果用户无权限则通过以下返回数据")
    @GetMapping("uauth")
    public String adminUAuth(){
        return JSON.toJSONString(new ResponseInfo<>(401, "用户无访问权限", false));
    }

    /**
     * 注销
     */
    @ApiOperation("退出")
    @GetMapping( "logout")
    public String logout() {
        return userServiceImpl.logout();
    }
}
